﻿using Loaf.Beans;
using Loaf.Tools;
using Prism.Commands;
using Prism.Mvvm;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace Loaf.ViewModels
{
    public class ReaderWindowModel : BindableBase
    {
        public string firstText;
        public string secondText;
        public List<BookDictionary> bookDictionaryList;
        public Visibility extendContentVisibility;

        public Context Context { get; set; }
        /// <summary>
        /// 当前章节内容
        /// </summary>
        public string FirstText { get => firstText; set { firstText = value; RaisePropertyChanged(); } }
        /// <summary>
        /// 下一章节内容
        /// </summary>
        public string SecondText { get => secondText; set { secondText = value; RaisePropertyChanged(); } }
        /// <summary>
        /// 当前阅读书籍
        /// </summary>
        public Book CurrBook { get => Context.BookSetting.LastBook; }
        /// <summary>
        /// 目录
        /// </summary>
        public List<BookDictionary> BookDictionaryList { get => bookDictionaryList; set { bookDictionaryList = value; RaisePropertyChanged(); } }
        /// <summary>
        /// 滚动器，在ReaderWindow中赋值
        /// </summary>
        public ScrollViewer ScrollViewer { get; set; }
        /// <summary>
        /// 打开或关闭附加内容：目录、字体设置
        /// </summary>
        public DelegateCommand ExtendContentCommand { get; set; }
        /// <summary>
        /// 附加内容的显示或隐藏
        /// </summary>
        public Visibility ExtendContentVisibility { get => extendContentVisibility; set { extendContentVisibility = value; RaisePropertyChanged(); } }
        /// <summary>
        /// 选择章节
        /// </summary>
        public DelegateCommand<BookDictionary> SelectChapterCommand { get; set; }


        public ReaderWindowModel(Context context)
        {
            Context = context;
            ExtendContentVisibility = Visibility.Collapsed;
            ExtendContentCommand = new DelegateCommand(ExtendContentAction);
            SelectChapterCommand = new DelegateCommand<BookDictionary>(SelectChapterAction);
        }

        /// <summary>
        /// 扩展功能，目录、字体大小、字体颜色
        /// </summary>
        public void ExtendContentAction()
        {
            ExtendContentVisibility = ExtendContentVisibility == Visibility.Visible ? Visibility.Collapsed : Visibility.Visible;

            if (ExtendContentVisibility == Visibility.Collapsed)
                Context.WriteContext();
        }

        /// <summary>
        /// 加载
        /// </summary>
        public void OnLoaded()
        {
            try
            {
                Task.Run(() =>
               {
                   if (BookDictionaryList == null)
                       BookDictionaryList = HtmlTool.GetHtmlDictionary(CurrBook.DictUrl, CurrBook.WebsiteType);

                   FirstText = BookDictionaryList[CurrBook.CurrIndex].Title + "\n";
                   FirstText += HtmlTool.GetHtmlText(BookDictionaryList[CurrBook.CurrIndex].Url, CurrBook.WebsiteType);

                   Task.Run(() =>
                   {
                       SecondText = BookDictionaryList[CurrBook.CurrIndex + 1].Title + "\n";
                       SecondText += HtmlTool.GetHtmlText(BookDictionaryList[CurrBook.CurrIndex + 1].Url, CurrBook.WebsiteType) + "\n";
                   });

                   App.Current.Dispatcher.Invoke(() =>
                   {
                       ScrollViewer.ScrollToVerticalOffset(CurrBook.Offset);
                   });
               });
            }
            catch { }
        }

        /// <summary>
        /// 获取下一章节
        /// </summary>
        public void GetNextChapter()
        {
            try
            {
                FirstText = SecondText;
                CurrBook.CurrIndex++;
                var url = BookDictionaryList[CurrBook.CurrIndex].Url;
                if (CurrBook.CurrIndex < BookDictionaryList.Count - 2)
                {
                    Task.Run(() =>
                    {
                        SecondText = BookDictionaryList[CurrBook.CurrIndex + 1].Title + "\n";
                        SecondText += HtmlTool.GetHtmlText(BookDictionaryList[CurrBook.CurrIndex + 1].Url, CurrBook.WebsiteType) + "\n";
                    });
                }
                else
                {
                    SecondText = "";
                }
            }
            catch { }
        }

        /// <summary>
        /// 选择章节
        /// </summary>
        /// <param name="dictionary"></param>
        public void SelectChapterAction(BookDictionary dictionary)
        {
            CurrBook.CurrIndex = BookDictionaryList.IndexOf(dictionary);
            CurrBook.Offset = 0;
            OnLoaded();
        }
    }
}
